package com.xydpeng.rpc.protocol;

import com.xydpeng.rpc.common.utils.HashUtil;
import lombok.Getter;
import lombok.Setter;

import java.lang.reflect.Method;

/**
 * @author xydpeng
 * @version v1.0
 * @date: 2023/4/10
 * @description:
 */
@Getter
@Setter
public class HandlerMeta<T> {
    private int handlerId;

    private Class<?> interfaceClass;
    private transient T ref;
    /**
     * interfaceClass中的method
     */
    private Method method;
    private Class<?>[] parameterTypes;
    private Class<?> returnType;
    private String fullMethodName;
    private HandlerInvoker invoker;

    public HandlerMeta(Class<?> interfaceClass, T ref, Method method) {
        this.interfaceClass = interfaceClass;
        this.ref = ref;
        this.method = method;
        this.parameterTypes = method.getParameterTypes();
        this.returnType = method.getReturnType();
        StringBuilder builder = new StringBuilder();
        builder.append(interfaceClass.getName()).append(".");
        builder.append(method.getName()).append("(");
        if (parameterTypes != null && parameterTypes.length > 0) {
            for (int i = 0; i < parameterTypes.length; i++) {
                builder.append(parameterTypes[i].getName());
                if (i != parameterTypes.length - 1) {
                    builder.append(",");
                }
            }
        }
        builder.append(" )");
        this.fullMethodName = builder.toString();
        this.handlerId = HashUtil.hashCode(fullMethodName);
    }
}
